package com.dtstack.rocketmq.listener;

import com.google.common.eventbus.AsyncEventBus;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Component;

/**
 * GrainCostEventPublisher
 *
 * <p>
 *     事件发布者：在你的Spring组件中注册监听器到EventBus。
 * </p>
 *
 * @author chuanbai
 * @date 2024/11/11
 */
@Component
public class GrainCostEventPublisher {

    @Resource
    private AsyncEventBus asyncEventBus;
    @Resource
    private GrainCostEventListener grainCostEventListener;

    public GrainCostEventPublisher(AsyncEventBus asyncEventBus, GrainCostEventListener grainCostEventListener) {
        this.asyncEventBus = asyncEventBus;
        this.grainCostEventListener = grainCostEventListener;
    }

    @PostConstruct
    public void init() {
        asyncEventBus.register(grainCostEventListener);
    }

    @PreDestroy
    public void destroy() {
        asyncEventBus.unregister(grainCostEventListener);
    }

    public void postGrainCostEvent(String message) {
        asyncEventBus.post(new GrainCostEvent(message));
    }
}
